import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;


public class MapCachedTickCounter extends TickCounter {

	public MapCachedTickCounter() throws NoSuchAlgorithmException, InvalidKeyException,
	NoSuchPaddingException {
		super();
		cache = new ConcurrentHashMap<byte[], byte[]>();
	}

	ConcurrentHashMap<byte[], byte[]> cache;
	
	private boolean cmpArrs(byte[] a, byte[] b) {
		boolean isEqual = true;
		for (int i = 0; i < a.length; ++i)
			if (a[i] != b[i]) return false;
		return isEqual;
	}
	
	@Override
	public byte[] cipher(byte[] in) throws IllegalBlockSizeException, BadPaddingException {
		in = Arrays.copyOf(in, in.length);
		byte[] out = cache.get(in);
		if (out != null) return out;
		
		out = super.cipher(in);
		cache.putIfAbsent(in, out);
		return out;
	}
}
